<template>
  <div class="dialog">
    <el-dialog width="60%" :title="title" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="disabled">
        <el-form-item label="广告标题：" prop="title">
          <el-input v-model="form.title" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="投放位置：" prop="deliveryAddressList">
          <el-checkbox-group v-model="form.deliveryAddressList" clearable>
            <el-checkbox v-for="item in locationOptions" :key="item.value" :label="item.value">{{ item.label }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="权重：" prop="weight">
          <el-col :span="12">
            <el-input v-model="form.weight" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="投放类型：" prop="deliveryType">
          <el-radio-group v-model="form.deliveryType">
            <el-radio :label="0">自定义推荐</el-radio>
            <!--              <el-radio :label="1">产品推荐</el-radio>-->
            <!--              <el-radio :label="2">活动推荐</el-radio>-->
            <!--              <el-radio :label="3">抽奖推荐</el-radio>-->
            <!--              <el-radio :label="4">优惠卷推荐</el-radio>-->
            <!--              <el-radio :label="5">课程推荐</el-radio>-->
          </el-radio-group>
        </el-form-item>
        <el-form-item label="广告图片：" prop="imgUrl">
          <UploadFile :file-list="form.imgUrl" @change="uploadHandle($event, 'imgUrl')" />
        </el-form-item>
        <el-form-item label="URL链接：">
          <el-input v-model="form.forwardUrl" placeholder="外链第三方地址需要第三方域名配置校验文件" clearable />
        </el-form-item>
        <el-form-item label="投放时间：" prop="dateRange">
          <el-date-picker
            v-model="form.dateRange"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          />
        </el-form-item>
        <el-form-item label="备注信息">
          <el-input v-model="form.remark" type="textarea" rows="5" placeholder="请输入" clearable />
        </el-form-item>
      </el-form>
      <div v-if="!id" slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">立即发布</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UploadFile from '@/components/UploadFile/index.vue'
import { ADVERT_LOCATION_LIST } from '@/utils/enum'
import { createBanner, getBannerDetail } from '@/api/advert'
export default {
  name: 'Addbanner',
  components: {
    UploadFile
  },
  props: {
    id: {
      type: [String, Number],
      default: ''
    }
  },

  data() {
    return {
      loading: false,
      locationOptions: ADVERT_LOCATION_LIST,
      form: {
        title: '',
        deliveryAddressList: [],
        weight: '',
        deliveryType: '',
        imgUrl: '',
        forwardUrl: '',
        dateRange: '',
        remark: ''
      },
      rules: {
        title: [{ required: true, message: '请输入广告标题', trigger: ['change', 'blur'] }],
        deliveryAddressList: [{ required: true, message: '请选择投放位置', trigger: ['change'] }],
        weight: [{ required: true, message: '请输入权重', trigger: ['change', 'blur'] }],
        deliveryType: [{ required: true, message: '请选择投放类型', trigger: ['change'] }],
        imgUrl: [{ required: true, message: '请上传广告图片', trigger: ['change'] }],
        dateRange: [{ required: true, message: '请选择投放时间', trigger: ['change', 'blur'] }]
      }
    }
  },

  computed: {
    title() {
      if (this.id) {
        return '广告详情'
      } else {
        return '新增广告'
      }
    },
    disabled() {
      return !!this.id
    }
  },

  created() {
    if (this.id) {
      this.getDetail()
    }
  },

  methods: {

    // 获取详情
    getDetail() {
      const data = {
        advertId: this.id
      }
      getBannerDetail(data).then(res => {
        const { startTime, endTime } = res
        res.dateRange = [startTime, endTime]
        this.form = res
      }).catch(() => {})
    },

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const [startTime, endTime] = this.form.dateRange || []
          const data = {
            ...this.form,
            startTime,
            endTime
          }
          createBanner(data).then(() => {
            this.$message.success('发布成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },

    // 上传图片处理
    uploadHandle(payload, name) {
      this.form[name] = payload.fileList[0]?.url
      this.$refs.form.validateField(name)
    }
  }

}
</script>

<style lang="scss" scoped></style>
